<?php
namespace Admin\Controller;
use Common\Controller\BackendController;

class KeywordsController extends BackendController
{
    public function index()
    {
        $model = D('keywords');
        $datas = $model->select();
        $this->assign('datas',$datas);
        $this->display();
    }

    public function store()
    {
        if(IS_POST){
            $model = D('keywords');
            $data['order'] = I('post.order');
            $data['keyword'] = I('post.keyword');
            $data['hot'] = I('post.hot');
            $data['is_show'] = I('post.is_show');
            $result = $model->add($data);
            if ($result) {
                $this->success('操作成功！', U('index'));
            } else {
                $this->success('操作失败！', U('index'));
            }
        }
    }

    public function edit($id)
    {
        $model = D('keywords');
        $data = $model->find($id);
        if (IS_POST) {
            $data['order'] = I('post.order');
            $data['keyword'] = I('post.keyword');
            $data['hot'] = I('post.hot');
            $data['is_show'] = I('post.is_show');
            $result = $model->where(['id'=>I('post.id')])->save($data);
            if ($result) {
                $this->success('操作成功！', U('index'));
            } else {
                $this->success('操作失败！', U('index'));
            }

        }

        $this->assign('data',$data);
        $this->display();
    }

    public function update()
    {
        $model = D('keywords');
        if(IS_POST) {
            $show = I('post.show');
            if ($show == 1) {
                $where['is_show'] = 0;
            } else {
                $where['is_show'] = 1;
            }
            $id = $model->where(['id' => I('post.id')])->save($where);
            if ($id) {
                $this->jsonReturn([
                    'msg' => 'success',
                    'state' => 200
                ]);
            } else {
                $this->jsonReturn([
                    'msg' => 'error',
                    'state' => 201
                ]);
            }
        }
    }

    public function destroy()
    {
        $model = D('keywords');
        $model->where(['id'=>I('post.id')])->delete();
    }
}